//
//  FansPresentView.m
//  LiveStar
//
//  Created by 漪珊 on 2017/1/11.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "FansPresentView.h"
#import "PresentCell.h"
#import "FPresentListModel.h"

@interface FansPresentView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    NSArray *_normalArr;
    NSArray *_selectedArr;
}
@property (nonatomic, copy) NSString *cid;
@end
@implementation FansPresentView
static NSString *presentCellId = @"presentCellId";

- (void)awakeFromNib {
    [super awakeFromNib];
    self.cid = @"";
    self.presentArray = [NSMutableArray arrayWithCapacity:1];
    _normalArr = @[@"flower_1_normal",@"flower_5_normal",@"flower_3_normal",@"flower_4_normal",@"flower_2_normal",@"flower_6_normal"];
    _selectedArr = @[@"flower_1_selected",@"flower_5_selected",@"flower_3_selected",@"flower_4_selected",@"flower_2_selected",@"flower_6_selected"];
    kButtonRadius(self.sendPresentBtn, self.sendPresentBtn.height/2);
    UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc] init];
    flowLaout.itemSize = CGSizeMake(106.5, 127*0.5);
    flowLaout.minimumLineSpacing = 0;        //纵向间距
    flowLaout.minimumInteritemSpacing = 0;  //横向间距
    flowLaout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.presentCollectionView.collectionViewLayout = flowLaout;
    [self addSubview:self.presentCollectionView];
    self.presentCollectionView.delegate = self;
    self.presentCollectionView.dataSource = self;
    self.presentCollectionView.backgroundColor = [UIColor clearColor];
    self.presentCollectionView.bounces = NO;
    self.presentCollectionView.showsHorizontalScrollIndicator = NO;
    [self.presentCollectionView registerNib:[UINib nibWithNibName:@"PresentCell" bundle:nil] forCellWithReuseIdentifier:@"presentCellId"];
}

#pragma mark -- UICollectionViewDelegate, UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PresentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"presentCellId" forIndexPath:indexPath];
    FPresentListModel *model = _presentArray[indexPath.row];
    switch (indexPath.row) {
        case 0:
            if (model.isSelected) {
                cell.presentImg.image = [UIImage imageNamed:@"flower_1_selected"];
            } else {
                cell.presentImg.image = [UIImage imageNamed:@"flower_1_normal"];
            }
            break;
        case 1:
            if (model.isSelected) {
                cell.presentImg.image = [UIImage imageNamed:@"flower_5_selected"];
            } else {
                cell.presentImg.image = [UIImage imageNamed:@"flower_5_normal"];
            }
            break;
        case 2:
            if (model.isSelected) {
                cell.presentImg.image = [UIImage imageNamed:@"flower_3_selected"];
            } else {
                cell.presentImg.image = [UIImage imageNamed:@"flower_3_normal"];
            }
            break;
        case 3:
            if (model.isSelected) {
                cell.presentImg.image = [UIImage imageNamed:@"flower_4_selected"];
            } else {
                cell.presentImg.image = [UIImage imageNamed:@"flower_4_normal"];
            }
            break;
        case 4:
            if (model.isSelected) {
                cell.presentImg.image = [UIImage imageNamed:@"flower_2_selected"];
            } else {
                cell.presentImg.image = [UIImage imageNamed:@"flower_2_normal"];
            }
            break;
        case 5:
            if (model.isSelected) {
                cell.presentImg.image = [UIImage imageNamed:@"flower_6_selected"];
            } else {
                cell.presentImg.image = [UIImage imageNamed:@"flower_6_normal"];
            }
            break;
            
        default:
            break;
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    for (int i = 0; i<6; i++) {
        FPresentListModel *model = _presentArray[i];
        if (i==indexPath.row) {
            model.isSelected = YES;
            self.cid = KString(model.c_id);
        } else {
            model.isSelected = NO;
        }
       [self.presentCollectionView reloadData];
    }
}

#pragma mark -- 发送礼物
- (IBAction)sendPresent:(id)sender {
    [self.delegate sendPresentToStarWithCid:self.cid];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
